/*
 * @Author: szx
 * @Date: 2022-02-23 12:48:16
 * @LastEditTime: 2022-02-23 12:51:40
 * @Description:
 * @FilePath: \leetcode\1-99\19\19_2.js
 */
function ListNode(val, next) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
}

/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
    const prev = new ListNode(-1, head);
    let l1 = prev;
    let l2 = prev;
    for (let i = 0; i < n; i++) l2 = l2.next;
    while (l2) {
        l1 = l1.next;
        l2 = l2.next;
    }
    l1.next = l1.next.next;
    return prev.next;
};
